package com.yunxiangyue

import android.content.Context
import android.os.Bundle
import android.os.Handler
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.ViewPager
import com.yunxiangyue.app.R

class recommend_fragement : Fragment(){

    var mHandler= Handler()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_recommend_fragement, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        var imgList= ArrayList<Int>()
        imgList.add(R.drawable.looper_1)
        imgList.add(R.drawable.looper_3)
        imgList.add(R.drawable.looper_4)
        imgList.add(R.drawable.looper_2)
        var looperAdapter=looperPageAdapter()
        looperAdapter.setData(imgList)
        val looper_img = view.findViewById<ViewPager>(R.id.looper_img)
        looper_img.adapter=looperAdapter
        looper_img.setCurrentItem(looperAdapter.getMpicsSize()*100,true)
        mHandler= Handler()


    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        val framLa=framlayoutActivity()
        framLa.setHandler(mHandler)
    }

    override fun onAttachFragment(childFragment: Fragment) {
        super.onAttachFragment(childFragment)
        mHandler.post(mLooperTask)
    }

    override fun onDetach() {
        super.onDetach()
        mHandler.removeCallbacks(mLooperTask)
    }


    private var mLooperTask:Runnable= Runnable{
        run {
            val looper_img = view?.findViewById<ViewPager>(R.id.looper_img)
            var currentItem = looper_img?.currentItem ?: 0
            looper_img?.setCurrentItem(currentItem++,true)
            mHandler.postDelayed(mLooperTask,1000)
        }
    }

    fun animation(){

    }




    companion object {
        fun newInstance() = recommend_fragement()
    }
}